//
//  System.cpp
//  WiFiMACSim
//
//  Created by Christopher Kemsley on 3/31/12.
//  Copyright (c) 2012 iAppolopolis. All rights reserved.
//

#include "System.hpp"


SimNamespaceBegin




System :: System ()
:	mCurrentSlot ( 0 )
	{
	RunLoop::currentRunLoop() . addSource ( this , RunLoop::kModeTime ) ;
	}
System :: ~System ()
	{
	// Singleton. Don't unregister.
	//RunLoop::currentRunLoop() . removeSource ( *this , RunLoop::kModeTime ) ;
	}

void	System :: runFromRunLoop ( RunLoop & sender , RunLoop::Mode mode )
	{
	mCurrentSlot ++ ;
	}





System&		System :: shared ()
	{
	static System s ;
	return s ;
	}



// Equivalent of "currentTime"
UInteger	System :: currentSlot ()
	{
	return mCurrentSlot ;
	}





// Make sure it's loaded before main

PreMainInitializer(SystemInit,{
	System :: shared () ;
})







SlotTimer :: SlotTimer ()
:	mFireDate ( UIntegerMax )
	{
	
	}
SlotTimer :: ~SlotTimer ()
	{
	
	}

// if now=1 and slots=2, will fire at slot=4
void	SlotTimer :: skipNext	( UInteger slots )
	{
	mFireDate = System::shared().currentSlot() + slots + 1 ;
	}
void	SlotTimer :: skip		( UInteger slots )
	{
	mFireDate = System::shared().currentSlot() + slots ;
	}


void	SlotTimer :: reset ()
	{
	mFireDate = UIntegerMax ;
	}
bool	SlotTimer :: isTime ()
	{
	if ( mFireDate <= System::shared().currentSlot() )
		{
		mFireDate = UIntegerMax ;
		return true ;
		}
	return false ;
	}
bool	SlotTimer :: isReset ()
	{
	return mFireDate == UIntegerMax ;
	}




SimNamespaceEnd
